<?php
/**
 * Introduction:  提现逻辑
 * @author: 杨陈鹏
 * @email: yangchenpeng@cdlinglu.com
 */

namespace Common\Logic;

use Common\Model;

class WithdrawalsLogic
{

    static public $model;

    public function __construct()
    {

        self::$model = new Model\WithdrawalsModel();

    }
    /**
     * Introduction:  申请提现
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function create($data,$uid)
    {

        $member_logic           = D('Common/Member','Logic');

        if( intval(floatval($data['money']) * 100) <= 0 )
            return ['code' => 300, 'msg' => '请输入正确的金额'];

        $member_account         = $member_logic::$model
                                            ->where(['uid' => $uid])
                                            ->field('balance,frozen')
                                            ->find();


        $datas['money']         = intval(floatval($data['money']) * 100);
        $datas['create_time']   = NOW_TIME;
        $datas['status']        = 1;
        $datas['uid']           = $uid;

        if( ($member_account['balance'] - $member_account['frozen'] -  $datas['money'])  < 0)
            return ['code' => 300, 'msg' => '余额不足!'];

        if(self::$model->add($datas)){

            //提现申请成功,冻结资金
            $member_logic::$model->where(['uid' => $uid])->setInc('frozen',$datas['money']);

            return ['code' => 200, 'msg' => '申请成功,等待客服处理'];

        }

        return ['code' => 300, 'msg' => '提现失败'];



    }
    /**
     * Introduction:  提现记录
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function getData($where, $filed = '*', $page, $nums, $order = "status asc,create_time desc")
    {

        $is_admin = $where['is_admin'];

        if($where['is_admin'])
            unset($where['is_admin']);

        $data = self::$model
                        ->where($where)
                        ->field($filed)
                        ->page($page,$nums)
                        ->order($order)
                        ->select();

        $total = self::$model
            ->where($where)
            ->count();

        if(!$data)
        {

            if($page == 1)
                return ['code' => 200, 'msg' => '您还没有提现记录!'];

            return ['code' => 200 ,'msg' => '后面没有了!','data' => []];

        }

        foreach($data as $k => $v)
            $data[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

        if($is_admin === 'yes'){

            $member_logic = D('Common/Member','Logic');

            foreach($data as $k => $v){

                $member_info = $member_logic::$apply_dis
                    ->where(['uid' => $v['uid']])
                    ->field('name,mobile,alipay_account')
                    ->find();

                $data[$k] = array_merge($data[$k],$member_info);

            }

        }

        return ['code' => 200, 'data' => $data,'total' =>$total];

    }



}